package com.jin.spirng_boot_learn.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy {

    public interface Person {
        void sing(String name);

        void dance(String name);
    }

    public static class XiaoMing implements Person {

        @Override
        public void sing(String name) {

            System.out.println("小明唱" + name);
        }

        @Override
        public void dance(String name) {

            System.out.println("小明跳" + name);

        }
    }


    public static void main(String[] args) {

        XiaoMing xiaoMing = new XiaoMing();

        Person o = (Person) Proxy.newProxyInstance(XiaoMing.class.getClassLoader(),XiaoMing.class.getInterfaces(), new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                //如果别人想要让小明唱歌
                if (method.getName().equals("sing")) {

                    System.out.println("给1000万来再唱");

                    //实际上唱歌的还是小明
                    method.invoke(xiaoMing, args); // 用实例唤醒
                }
                return null;
            }
        });
        o.sing("1111");

    }
}
